<template>
  <div class="y-conter">
    <div class="y-div-1">
      <div class="y-title">
        <span class="y-font-1">项目亮点</span>
        <el-button type="submit" style="margin-left:1000px" @click="addJiedian()">添加亮点</el-button>
      </div>
      <!-- 表格部分 -->
      <div class="y-table">
        <el-table
          :border="true"
          :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
          ref="multipleTable"
          :data="currList"
          tooltip-effect="dark"
          style="width: 100%, text-align: center "
          @selection-change="handleSelectionChange"
        >
          <el-table-column align="center" type="index" :index="indexFn" label="序号" width="100px"></el-table-column>
          <el-table-column
            label="亮点说明"
            prop="lightspot_text"
            hight="30px"
            align="center"
            show-overflow-tooltip
          ></el-table-column>
          <!-- <el-table-column label="	计划完成时间" prop="node_planend" align="center" show-overflow-tooltip></el-table-column>
          <el-table-column label="实际完成时间" prop="node_realend" align="center" show-overflow-tooltip></el-table-column>-->
          <el-table-column label="实景图" prop="company_code" align="center" show-overflow-tooltip>
            <!-- <template slot-scope="scope">
              <img :src="scope.row.buildMaterial.material_url" alt style="width:30px;height:30px" />
            </template> -->
              <template slot-scope="scope">
              <el-popover placement="top-start" trigger="click">
                <a :href="scope.row.buildMaterial.material_url">
                  <img :src="scope.row.buildMaterial.material_url" alt style="width:300px;height:300px" />
                </a>
                <img
                  :src="scope.row.buildMaterial.material_url"
                  alt
                  style="width:70px;height:50px;cursor:pointer"
                  slot="reference"
                />
              </el-popover>
            </template>
          </el-table-column>

          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
             
              <el-button type="text" @click="showConfirm(scope.$index, scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-dialog title="新增项目节点:" :visible.sync="dialogFormVisible" width="450px">
          <el-form :model="form" ref="updataForm">
            <el-form-item label="亮点说明:" :label-width="formLabelWidth">
              <el-input v-model="form.lightspot_text" autocomplete="off" class="padding"></el-input>
            </el-form-item>
            <el-form-item label="亮点名称:" :label-width="formLabelWidth">
              <el-input v-model="form.buildMaterial.material_name" autocomplete="off" class="padding"></el-input>
            </el-form-item>
            <el-form-item label="附件:" :label-width="formLabelWidth">
              <el-input v-model="form.buildMaterial.material_url" autocomplete="off" class="padding"></el-input>
              <el-upload
                action="api/pic/load"
                :headers="userToken"
                :on-preview="handlePreview"
                :on-remove="handleRemove"
                :on-success="handleSuccess"
                :before-remove="beforeRemove"
                multiple
                :limit="3"
                :on-exceed="handleExceed"
                :show-file-list="false"
              >
                <el-button type="text" class="clicksub" size="medium">
                  上传
                  <i class="el-icon-upload el-icon--right"></i>
                </el-button>
              </el-upload>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="quaoxiao(form)">取 消</el-button>
            <el-button type="primary" @click="SureEdit('updataForm')">确 定</el-button>
          </div>
        </el-dialog>

      </div>

      <!-- 分页器 -->
      <div class="block">
        <el-pagination
          background
          layout="prev, pager, next"
          :total="tableData.length"
          :page-size="pageSize"
          @current-change="changePage"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
import http from "../api";
export default {
  data() {
    return {
      userToken: {}, //usertoken的值
      // JinYong:true,
      finput: [],
      // 分页
      tableData: [], //总数居
      currPage: 1, //当前页
      pageSize: 7, //每页数据量
      currList: [], //当前展示的数据数组
      dialogFormVisible: false,
      dialogForm: false,
      form: {
        lightspot_text: "",
        buildMaterial: {
          material_name: "",
          material_url: "",
        },
      },
      xgForm: {
        node_id: "",
        node_name: "",
        node_planend: "",
        node_realend: "",
      },
      delForm: {
        lightspot_id: "",
        material_id: "",
      },
      formLabelWidth: "110px",
    };
  },
  methods: {

    // 表格里的删除
    showConfirm(index, row) {
      this.delForm.lightspot_id = row.lightspot_id;
      this.delForm.material_id = row.material_id;
      console.log("删除", this.delForm);
      // http.getBuildCompanyTypes  buildCompany/delBuildCompany
      http.delBuildLightspot(this.delForm).then((r) => {
        console.log(r);
        http.getBuildLightspots({}).then((r) => {
          console.log(r);
          this.tableData = r.data;
          console.log("数据", r.data);
          this.setCurrList(1);
        });
        alert(r.msg);
        // this.setCurrList(1);
      });
    },
    // 编辑的确认
    // 添加里的提交按钮
    SureEdit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.dialogFormVisible = false;
          http.addBuildLightspot(this.form).then((r) => {
            console.log("这是编辑的", r);
            http.getBuildLightspots({}).then((res) => {
              this.tableData = res.data;
              this.setCurrList(this.currPage);
            });
            alert(r.msg);
          });
        } else {
          this.$message({
            showClose: true,
            message: "修改失败",
            type: "error",
          });
          return false;
        }
      });
    },
    // 编辑里的取消按钮
    quaoxiao(form) {
      console.log(form);
      this.dialogFormVisible = false;
      // 点取消后清空
      for (let key in form) {
        form[key] = "";
      }
    },
    // 添加
    addJiedian() {
      this.dialogFormVisible = true;
    },
    // 分页
    setCurrList(index) {
      this.currList = this.tableData.slice(
        (index - 1) * this.pageSize,
        index * this.pageSize
      );
      // console.log('当前页',index);
    },
    //改变当前页
    changePage(val) {
      this.currPage = val;
      this.setCurrList(val);

      // console.log(val);
    },
    handleSelectionChange(val) {
      console.log(val);
      this.newData = val;
      console.log(this.newData);
    },
    // 表格序号
    indexFn(index) {
      index = index + 1 + (this.currPage - 1) * this.pageSize;
      return index;
    },
    //重置按钮
    resetForm(formName) {
      this[formName] = {};
      this.$refs[formName].resetFieldbeforeRemoves();
    },
    // 上传
    // 成功
    handleSuccess(res, file, fileList) {
      console.log("文件", res);
      this.form.buildMaterial.material_url = res.name;
      this.$notify.success({
        title: "成功",
        message: `文件上传成功`,
      });
    },
    // 删除
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${
          files.length + fileList.length
        } 个文件`
      );
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },
  },
  mounted() {
    this.userToken.userToken = sessionStorage.getItem("userToken");
    http.getBuildLightspots({}).then((r) => {
      console.log(r);
      this.tableData = r.data;
      console.log("数据", r.data);
      this.setCurrList(1);
    });
  },
};
</script>
<style  scoped>
.padding >>> .el-input__inner {
  width: 178px;
  height: 30px;
  padding-left: 5px !important;
}
.y-conter {
  width: 100%;
  height: 660px;
  background: #fff;
}
.y-div-1 {
  width: 98%;
  height: 580px;
  margin: 0px auto;
}

.y-title {
  width: 100%;
  height: 60px;
  line-height: 60px;
  /* background: red; */

  /* padding-top: 20px; */
}
.y-font-1 {
  font-size: 15px;
  font-weight: bold;
  /* padding-left: 15px; */
}
.el-input >>> .el-input__inner {
  width: 240px !important;
  height: 30px;
  /* margin-left: 10px; */
}
.el-select >>> .el-input__inner {
  width: 240px !important;
  height: 30px;
}
.block {
  margin-left: 85%;
  margin-top: 15px;
}
</style>